//
//  LykZhuanTiView.m
//  GiftSay
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "LykZhuanTiView.h"
#import "HomeSecondBannerModel.h"
@interface LykZhuanTiView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *secondBannerData;
@end

@implementation LykZhuanTiView

-(instancetype)initWithFrame:(CGRect)frame withBannerData:(NSMutableArray *)secondBannerData {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
        
        self.secondBannerData = [[NSMutableArray alloc] initWithArray:secondBannerData];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(self.width / 4 - 15, self.width / 4 - 15);
        flowLayout.minimumInteritemSpacing = 10;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.width - 20, self.height - 20) collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.bounces = NO;
        //    隐藏滚动条
        self.collectionView.showsVerticalScrollIndicator = FALSE;
        self.collectionView.showsHorizontalScrollIndicator = FALSE;
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [self addSubview:self.collectionView];
        
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            self.backgroundColor = kNightModeColor;
            self.collectionView.backgroundColor = kNightModeColor;
            ;
        } else {
            self.backgroundColor = [UIColor whiteColor];
            self.collectionView.backgroundColor = [UIColor whiteColor];
        }
    }
    return self;
}

#pragma mark -- collectionview协议
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds];
    [imageView setImageWithURL:[NSURL URLWithString:[self.secondBannerData[indexPath.row] image_url]] placeholder:nil];
    [cell addSubview:imageView];
    return cell;
}
-(void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.backgroundColor = kNightModeColor;
        self.collectionView.backgroundColor = kNightModeColor;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.collectionView.backgroundColor = [UIColor whiteColor];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *url = nil;
    if (indexPath.item == 0) {
        url = nil;
    } else if (indexPath.item == 1) {
        url = @"http://www.liwushuo.com/posts/1030588/content";
    } else if (indexPath.item == 2) {
        url = @"";
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

@end
